var express = require("express");
var router = express.Router();

var {
  up,
  getSupplier,
  delSupplier,
  upSupplier,
  upS,
  getOrder,
  delOrder,
  upOrder
} = require("../service/supplierService");

//录入供应商
router.post("/supplier", async function(req, res, next) {
  const { name, addr, phone, web, note, state } = req.body; //获取材料
  const data = await up({
    name,
    addr,
    phone,
    web,
    note,
    state
  });
  res.send({
    rows: data
  });
});

//查找供应商
router.get("/getSupplier", async function(req, res, next) {
  const data = await getSupplier();
  res.send({
    rows: data
  });
});

//查找订单
router.get("/getOrder", async function(req, res, next) {
  const data = await getOrder();
  res.send({
    rows: data
  });
});

//删除供应商
router.post("/delSupplier", async function(req, res, next) {
  const { _id } = req.body;
  const data = await delSupplier({ _id });

  res.send({
    rows: data
  });
});

//删除订单
router.post("/delOrder", async function(req, res, next) {
  const { _id } = req.body;
  const data = await delOrder({ _id });
  res.send({
    rows: data
  });
});

//修改供应商
router.patch("/:_id", async function(req, res, next) {
  const { _id } = req.params;
  const { name, addr, phone, note, web } = req.body;

  const data = await upSupplier(_id, { name, addr, phone, note, web });
  res.send({
    rows: data
  });
});
//修改订单
router.patch("/order/:_id", async function(req, res, next) {
  const { _id } = req.params;
  const { number, price } = req.body;

  const data = await upOrder(_id, {number, price  });
  res.send({
    rows: data
  });
});

//审核供应商
router.patch("/audit/:_id", async function(req, res, next) {
  const { _id } = req.params;
  const { state, comments } = req.body;

  const data = await upSupplier(_id, { state, comments });
  res.send({
    rows: data
  });
});

//增加供应商货品
router.post("/selectSuppliers", async function(req, res, next) {
  const { supplierId, goodsId, number, price } = req.body; //获取材料
  const data = await upS({
    supplierId,
    goodsId,
    number,
    price
  });
  res.send({
    rows: data
  });
});

module.exports = router;
